package com.edu.servlet;

import com.edu.service.AdminScheduleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/schedule/*")
public class AdminScheduleServlet extends HttpServlet {
    private AdminScheduleService adminScheduleService = new AdminScheduleService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if (pathInfo == null) {
            pathInfo = "/";
        }

        switch (pathInfo) {
            case "/list":
                showSchedule(request, response);
                break;
            default:
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    private void showSchedule(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取课程表数据
        request.setAttribute("scheduleMap", adminScheduleService.getSchedule());

        // 转发到课程表页面
        request.getRequestDispatcher("/admin/schedule-list.jsp").forward(request, response);
    }
}